package ifmo.group5512;


import ifmo.group5512.data.DataManager;
import ifmo.group5512.entity.Order;
import ifmo.group5512.util.ComplexityOfOrder;
import ifmo.group5512.util.ComplexityOfOrderService;

import java.util.ArrayList;
import java.util.List;

public class Optimizer {
    private static List<Order> orders = DataManager.getAllCurrentOrders();

    public static void optimize(){
        List<ComplexityOfOrder> complexityOfOrderList
                = new ArrayList<ComplexityOfOrder>(orders.size());
        for(Order order: orders){
            Integer complexity = DataManager.getComplexityForOrder(order);
            Integer daysToComplete = DataManager.getDaysToComplete(order);
            Integer waitingForMaterials = DataManager.getWaitingForMaterials(order);
            complexityOfOrderList.add(new ComplexityOfOrder(order,complexity,daysToComplete,waitingForMaterials));
        }
        List<ComplexityOfOrder> ordersWithBigWaitingTime
                = new ArrayList<ComplexityOfOrder>(orders.size());
        List<ComplexityOfOrder> ordersWithLowWaitingTime
                = new ArrayList<ComplexityOfOrder>(orders.size());
        for(ComplexityOfOrder complexityOfOrder: complexityOfOrderList){
            if(complexityOfOrder.getComplexity() <= complexityOfOrder.getWaitingForMaterials()){
                ordersWithBigWaitingTime.add(complexityOfOrder);
            }else{
                ordersWithLowWaitingTime.add(complexityOfOrder);
            }
        }
        ComplexityOfOrderService.sortOrders(ordersWithBigWaitingTime);
        ComplexityOfOrderService.sortOrders(ordersWithLowWaitingTime);

        orders = new ArrayList<Order>();
        for(ComplexityOfOrder complexityOfOrder:ordersWithLowWaitingTime){
            orders.add(complexityOfOrder.getOrder());
        }
        for(ComplexityOfOrder complexityOfOrder:ordersWithBigWaitingTime){
            orders.add(complexityOfOrder.getOrder());
        }

    }
}
